package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import org.greenrobot.eventbus.EventBus;

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Object[] objs = (Object[]) intent.getExtras().get("pdus");

        try {
            for (Object obj : objs) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
                String from = smsMessage.getOriginatingAddress();

                String body = smsMessage.getMessageBody();
                String text = "发送者:" + from + "  内容：" + body;

                Log.d("sms", body + from);
                NoticeBean bean = new NoticeBean();
                bean.setType(3);
                bean.setTitle("获取到短信" + from);
                bean.setTime(System.currentTimeMillis());
                bean.setNoticeContent(text);
                bean.setNotificationPkg("短信");
                EventBus.getDefault().post(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

